/*
 * jAutomation: Portable input Automation 
 * Copyright (C) 2012 Dominik Hensler.  
 * http://code.google.com/p/jautomation
 * 
 * This Program uses JNativeHook by Alexander Barker:
 * JNativeHook: Global keyboard and mouse hooking for Java.
 * Copyright (C) 2006-2012 Alexander Barker.  All Rights Received.
 * http://code.google.com/p/jnativehook/
 *
 * 
 * This file is part of JAutomation.

 * JAutomation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * JAutomation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with JAutomation.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jautomation.pdetect;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jautomation.core.Automation;
import org.jautomation.core.TimedEvent;

/**
 *
 * @author Dominik Hensler
 */
public class ScanReceiver {

    BufferedImage screen;
    Robot robot;    
    Automation _automation;
    
    public ScanReceiver(Automation automation){     
        try {
            this.robot=new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(ScanReceiver.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(-1);
        }
        this._automation=automation;
    }

    public void getScreenShot() {
        this.screen = robot.createScreenCapture(new Rectangle(
                            Toolkit.getDefaultToolkit().getScreenSize()));
    }

    public void comparePixel(WorkerContext _pcontext) {
        if (_pcontext.isFirstRun()) {
            _pcontext.setFirstRun(false);
            int pixel = screen.getRGB(_pcontext.getCoordP().x, _pcontext.getCoordP().y);
            _pcontext.setPreservedPixel(pixel);
        } else {
            int pixel = screen.getRGB(_pcontext.getCoordP().x, _pcontext.getCoordP().y);
            if (_pcontext.getPreservedPixel() != pixel) {
                System.out.println("pixel has changed!");                
                _automation.setEvent(new TimedEvent("pmonEvent"));
                _pcontext.setPreservedPixel(pixel);
            }
        } 
    }

    public void findPixel(WorkerContext pcontext) {
        getScreenShot();
        String[][] colors=pcontext.getColors();
        double[] compColor;
        double[] color=pcontext.getColor();
        pcontext.setColors(Converter.getRGBColorStrings(screen, 
                                               pcontext.getExcludeRange(), 
                                               pcontext.getScanResolution()));
        for(int i=0;i<pcontext.getColors().length;i++){
            for(int p=0;p<pcontext.getColors().length;p++){
                compColor = Converter.rgbString2LabDouble(colors[i][p]);
                if(compColor[0]==color[0]&&compColor[1]==color[1]&&compColor[2]==color[2]){
                    int x=i+pcontext.getExcludeRange()[0];
                    int y=p+pcontext.getExcludeRange()[1];
                    pcontext.setCoordP(new Point(x,y));
                    System.out.println("match!");                    
                }
            }
        }
    }
}
